﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using SurveySystem.Model;
using SurveySystem.Utility;

namespace SurveySystem.Repository.MySQL
{
    public class MssqlSurveyQuestionRepository : ISurveyQuestionRepository
    {

        public Question GetQuestionByID(int questionID)
        {
            string sql;
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            Question q = null;

            sql = "SELECT `question_id`, `survey_id`, `question_number`, `question_text`, `required`, `multi_select` \n" + //
                  "FROM `survey_question` WHERE `question_id`=@id";

            try
            {
                conn = new SqlConnection(Globals.ConnectionString);
                cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", questionID);

                conn.Open();
                dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    q = new Question();

                    q.QuestionID = dr.GetInt32(0);
                    q.SurveyID = dr.GetInt32(1);
                    q.QuestionNumber = dr.GetInt32(2);
                    q.QuestionText = dr.GetString(3);
                    q.Required = dr.GetBoolean(4);
                    q.MultiSelect = dr.GetBoolean(5);

                }
                else
                {
                    q = null;
                }

                conn.Close();
            }
            catch (SqlException ex)
            {
                // TODO: logging
                throw ex;
            }

            return q;
        }

        public List<Question> GetQuestionsForSurvey(int surveyID)
        {
            string sql;
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            List<Question> list = new List<Question>();

            sql = "SELECT `question_id`, `survey_id`, `question_number`, `question_text`, `required`, `multi_select` \n" + //
                  "FROM `survey_question` WHERE `survey_id`=@id";

            try
            {
                conn = new SqlConnection(Globals.ConnectionString);
                cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", surveyID);

                conn.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    Question q = new Question();

                    q.QuestionID = dr.GetInt32(0);
                    q.SurveyID = dr.GetInt32(1);
                    q.QuestionNumber = dr.GetInt32(2);
                    q.QuestionText = dr.GetString(3);
                    q.Required = dr.GetBoolean(4);
                    q.MultiSelect = dr.GetBoolean(5);

                    list.Add(q);

                }

                conn.Close();
            }
            catch (SqlException ex)
            {
                // TODO: logging
                throw ex;
            }

            return list;
        }

        public void AddQuestion(Question q)
        {
            throw new NotImplementedException();
        }

        public void UpdateQuestion(Question q)
        {
            throw new NotImplementedException();
        }

        public void DeleteQuestion(int questionID)
        {
            throw new NotImplementedException();
        }
    }
}
